/*
	Copyright (C) 2008 by Jean-FranÁois Geyelin
	jfgeyelin@gmail.com

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the
	Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/



#include "Bouncer.h"
#include "globals.h"
#include "FloatingMessage.h"

Bouncer::Bouncer(float x_, float y_): Enemy(x_,y_,16,27,1,10,14,14){
	numberOfFrame=8;
	frameToBeDrawn = 0;
	isInBallForm = false;
	timeLeftUntilNextFrame = 10;
	timeLeftUntilNewBallForm = 10;
	sprites = new SplittedSurface("bouncer.png",10);
	depth = ENNEMIES_DEPTH;
	typeOfEnemy = BOUNCER_EN;
}

Bouncer::~Bouncer(void) {
	delete sprites;
}

void Bouncer::handleCollisionWithObject(Object *o) {
	if (o==player) {
		player->setAlive(false);
	}
}


void Bouncer::setVxVy() {
	
	if (!isDead && abs((int)(player->getX() - this->x)) < 40) {
		if (player->spearIsHittingThisObject(this)) {
			mustBeDestroyed = true;
			player->increaseNumberOfPoints(100);
			new FloatingMessage(x+4,y,"p_100.png");			
		}
	}
	
	
	
	
	
	timeLeftUntilNextFrame--;
	if (!timeLeftUntilNextFrame) {
		frameToBeDrawn++;
		if (frameToBeDrawn >= numberOfFrame)
		frameToBeDrawn = 0;
		timeLeftUntilNextFrame = 10;
	}



	if (!isInBallForm) {
		if (timeLeftUntilNewBallForm)
		timeLeftUntilNewBallForm--;
		vx=0;

		if (hasGroundUnderneath()) {
		    if (frameToBeDrawn==6) {
		        int v = myRandomMaker->rand_int32();
		  	    vy=-(2+(v%4));
		    }
        }
		else
		vx = (x<player->getX()?1:-1);

		if (distance((Object*)player,this)<150*150 && timeLeftUntilNewBallForm==0) {
			ballVector = 4;
			if (x>player->getX())
			ballVector=-4;
			isInBallForm = true;
			ballTimeLeft = 50;
			ballIFrame=0;
			ballFrame=0;
		}
	}

	if (isInBallForm) {
		vx = ballVector;
		ballVector+=(ballVector<0?0.1:-0.1);
		ballIFrame++;
		if (ballIFrame==3) {
			ballIFrame=0;
			ballFrame++;
			if (ballFrame==2)
			ballFrame=0;
		}
		if (abs((int)ballVector)<0.5) {
			isInBallForm=false;
			frameToBeDrawn=0;
			timeLeftUntilNewBallForm=10;
		}
		else
		{
			frameToBeDrawn=8+ballFrame;
		}
	}
	//frameToBeDrawn = 0;
}

